﻿///////////////////////////////////////////////////////////////////////////////
//
//  This source is part of the  "Managed Task Framework.NET Runtime"  (MTF.NET Runtime).
//
//
//  Copyright (c) 2011, Multicore Consulting Limited (UK)
//  All rights reserved.
//  http://www.multicoreconsulting.co.uk
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//
//  -  Redistributions of source code must retain the above copyright
//     notice, this list of conditions and the following disclaimer.
//  -  Redistributions in binary form must reproduce the above copyright
//     notice, this list of conditions and the following disclaimer in
//     the documentation and/or other materials provided with the
//     distribution.
//  -  Neither the name of Multicore Consulting Limited nor the names of
//     its contributors may be used to endorse or promote products
//     derived from this software without specific prior written
//     permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MTFOrg.MTF.MTFRuntime;

namespace MTFOrg.MTF.MTFCore
{
    /// <summary>
    /// Helper class for manipulating dimension arrays.
    /// </summary>
    class DimensionHelper
    {
        /// <summary>
        /// Gets the number of elements from a set of dimensions.
        /// </summary>
        /// <param name="dimensions">Set of dimensions</param>
        /// <returns>Number of elements</returns>
        public static int GetNumElementsFromDimensions(int[] dimensions)
        {
            int numElements = 1;

            foreach (int dimension in dimensions) numElements *= dimension;

            return numElements;
        }

        /// <summary>
        /// Adds two dimension arrays together.
        /// </summary>
        /// <param name="dims1">First dimension array</param>
        /// <param name="dims2">Second dimension array</param>
        /// <returns>Added dimensions</returns>
        public static int[] AddDims(int[] dims1, int[] dims2)
        {
            List<int> dims = new List<int>();

            foreach (int dim in dims1)
                if (dim > 1) dims.Add(dim);
            foreach (int dim in dims2)
                if (dim > 1) dims.Add(dim);

            if (dims.Count == 0) dims.Add(1);

            return dims.ToArray();
        }

        /// <summary>
        /// Removes the specified number of most significant dimensions.
        /// </summary>
        /// <param name="dims">Dimensions array to remove dimensions from</param>
        /// <param name="numDims">Number of dimensions to remove</param>
        /// <returns>Truncated dimensions array</returns>
        public static int[] RemoveMostSignificantDims(int[] dims, int[] dimsToRemove)
        {
            int numDims = 0;
            if (dimsToRemove.Length > 0 && dimsToRemove[0] != 1)
                numDims = dimsToRemove.Length;

            int[] outDims = new int[dims.Length - numDims];

            for (int i = 0; i < dims.Length - numDims; i++)
                outDims[i] = dims[i + numDims];

            if (outDims.Length == 0)
                outDims = new int[] { 1 };

            return outDims;
        }

        /// <summary>
        /// Removes the specified number of least significant dimensions.
        /// </summary>
        /// <param name="dims">Dimensions array to remove dimensions from</param>
        /// <param name="numDims">Number of dimensions to remove</param>
        /// <returns>Truncated dimensions array</returns>
        public static int[] RemoveLeastSignificantDims(int[] dims, int[] dimsToRemove)
        {
            int numDims = 0;
            if (dimsToRemove.Length > 0 && dimsToRemove[0] != 1)
                numDims = dimsToRemove.Length;

            int[] outDims = new int[dims.Length - numDims];

            for (int i = 0; i < dims.Length - numDims; i++)
                outDims[i] = dims[i];

            if (outDims.Length == 0)
                outDims = new int[] { 1 };

            return outDims;
        }

        /// <summary>
        /// Gets the total dimensions of the specified task.
        /// </summary>
        /// <param name="task">Task to find total dimensions of</param>
        /// <returns>Total dimensions of specified task</returns>
        public static int[] GetTotalDimsFromTask(ITask task)
        {
            int[] totalDims = new int[] { 1 };

            ITask curTask = task;
            while (curTask != null)
            {
                totalDims = AddDims(curTask.Dimensions, totalDims);
                curTask = curTask.ParentTask;
            }

            return totalDims;
        }

        /// <summary>
        /// Calculates a set of multidimensional elements from a flat element.
        /// </summary>
        /// <param name="dims">Dimensions</param>
        /// <param name="elem">Flat element number</param>
        /// <returns>Multidimensional element numbers</returns>
        public static int[] GetElementsFromElement(int[] dims, int elem)
        {
            int[] elems = new int[dims.Length];

            int divisor = 1;
            for (int i = 0; i < dims.Length - 1; i++)
                divisor *= dims[i];

            for (int i = 0; i < dims.Length - 1; i++)
            {
                int curElem = elem / divisor;
                elem -= curElem * divisor;

                elems[dims.Length - 1 - i] = curElem;

                divisor /= dims[dims.Length - 2 - i];
            }
            elems[0] = elem;

            return elems;
        }

        /// <summary>
        /// Calculates a flat element from a set of multidimensional elements.
        /// </summary>
        /// <param name="dims">Dimensions</param>
        /// <param name="elems">Multidimensional element numbers</param>
        /// <returns>Flat element number</returns>
        public static int GetElementFromElements(int[] dims, int[] elems)
        {
            int elem = 0;

            int multiplier = 1;
            for (int i = 0; i < dims.Length; i++)
            {
                elem += elems[i] * multiplier;
                multiplier *= dims[i];
            }

            return elem;
        }
    }
}